package com.yaopaikeji.dao;

import com.yaopaikeji.constants.Constants;
import com.yaopaikeji.exception.DeleteException;
import com.yaopaikeji.exception.InsertException;
import com.yaopaikeji.exception.InvalidNullException;
import com.yaopaikeji.exception.UpdateException;
import com.yaopaikeji.mapper.UserMapper;
import com.yaopaikeji.model.User;
import com.yaopaikeji.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDao extends BaseDao<User>{

    private static Logger logger = LoggerFactory.getLogger(UserDao.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByPrimaryKey(int id) {

        return userMapper.findByPrimaryKey(id);
    }

    @Override
    public List<User> findAll() {

        return userMapper.findAll();
    }

    public User findByLoginId(String loginId){

        return userMapper.findByLoginId(loginId);
    }

    public User findByEmail(String email){

        return userMapper.findByEmail(email);
    }

    public User findByPhone(String phone){

        return userMapper.findByPhone(phone);
    }

    public boolean isLoginIdExists(String loginId){

        return userMapper.isLoginIdExists(loginId);
    }

    public boolean isEmailExists(String email){

        return userMapper.isEmailExists(email);
    }

    public boolean isPhoneExists(String phone){

        return userMapper.isPhoneExists(phone);
    }

    public User saveUser(User user){

        int re = userMapper.saveUser(user);
        if(re == 1)
            return user;

        logger.error("插入数据失败");
        throw new InsertException(String.format(Constants.INSERT_ERROR_FMT, "用户信息"));
    }

    public User updateUser(User user){

        if(user == null) {

            logger.error("更新数据：数据为空");
            throw new InvalidNullException();
        }

        int re = userMapper.updateUser(user);
        if(re == 1)
            return user;

        logger.error("更新数据出错");
        throw new UpdateException(String.format(Constants.UPDATE_ERROR_FMT, "用户"));
    }

    public boolean updateIsActiveByLoginId(String loginId,Integer is_active){

        User user = new User();
        user.setLoginId(loginId);
        user.setIsActive(is_active);
        user.setLastModifiedTime(TimeUtil.now());
        int re = userMapper.updateIsActiveByLoginId(user);

        if(re == 1)
            return true;

        logger.error("更新用户状态出错");
        throw new UpdateException(String.format(Constants.UPDATE_ERROR_FMT, "用户状态"));
    }

    public boolean updateIsActiveById(int id,Integer is_active){
        User user = new User();
        user.setId(id);
        user.setIsActive(is_active);
        user.setLastModifiedTime(TimeUtil.now());
        int re = userMapper.updateIsActiveById(user);

        if(re == 1)
            return true;

        logger.error("更新用户状态出错");
        throw new UpdateException(String.format(Constants.UPDATE_ERROR_FMT, "用户状态"));
    }

    public boolean deleteUserById(int id){

        int re = userMapper.deleteUserById(id);

        if(re == 1)
            return true;

        logger.error("删除用户出错");
        throw new DeleteException(String.format(Constants.DELETE_ERROR_FMT, "用户"));
    }
}
